package thread;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;

import bo.IsConnected;
import bo.UserFunctions;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.steam.splace.MainActivity;
import com.steam.splace.ProfileActivity;
import com.steam.splace.R;
import com.steam.splace.SearchActivity;
import com.steam.splace.SearchByAreaActivity;

import entity.Comment;
import entity.Place;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DoThread extends AsyncTask<ArrayList, Integer, Void> {
	private final int success_tag = 0;
	private final int login_tag = 1;
	private final int forgotpw_tag = 3;
	private final int signup_tag = 5;
	private final int addplace_tag = 7;
	private final int getComment_tag = 11;
	private final int addComment_tag = 12;
	private final int edituser_tag = 15;
	private final int report_tag = 16;
	private final int addfavorite_tag = 17;
	private final int addVote_tag = 18;
	private int tag = -1;
	private String pid;
	UserFunctions user;
	private ArrayAdapter<Comment> arrayAdapter;
	Activity contextParent;
	ProgressDialog progress;

	public DoThread(Activity ctx) {
		contextParent = ctx;
		progress = new ProgressDialog(ctx);
	}

	@Override
	protected void onPreExecute() {
		// Runs on the UI thread
		progress.setMessage("Vui lòng chờ");
		progress.setIndeterminate(true);
		progress.setCancelable(false);
		progress.show();
	}

	@Override
	protected Void doInBackground(ArrayList... params) {
		// TODO Auto-generated method stub
		IsConnected ic = new IsConnected(contextParent);
		if (ic.isConnectedToInternet() == true) {
			user = new UserFunctions();
			user.setActivity(contextParent);
			ArrayList<Place> flag = null;
			if (Integer.parseInt(params[0].get(0).toString()) == login_tag) {
				List<NameValuePair> pair = (List<NameValuePair>) params[0]
						.get(1);
				tag = login_tag;
				// flag=user.loginUser(pair,contextParent);
				publishProgress(user.loginUser(pair));
			}
			if (Integer.parseInt(params[0].get(0).toString()) == addVote_tag) {
				List<NameValuePair> pair = (List<NameValuePair>) params[0]
						.get(1);
				tag = addVote_tag;
				// flag=user.loginUser(pair,contextParent);
				System.out.println("vote:" + pair.get(0).getName() + "/"
						+ pair.get(0).getValue());
				System.out.println("vote:" + pair.get(1).getName() + "/"
						+ pair.get(1).getValue());
				publishProgress(user.sendVote(pair));
			} else if (Integer.parseInt(params[0].get(0).toString()) == forgotpw_tag) {
				List<NameValuePair> pair = (List<NameValuePair>) params[0]
						.get(1);
				tag = forgotpw_tag;
				// flag=user.resetPassword(pair);
				publishProgress(user.resetPassword(pair));
			} else if (Integer.parseInt(params[0].get(0).toString()) == addComment_tag) {
				List<NameValuePair> pair = (List<NameValuePair>) params[0]
						.get(1);
				tag = addComment_tag;
				// flag=user.resetPassword(pair);
				Log.v("goi addcomment", pair.get(0).getValue() + "/"
						+ pair.get(1).getValue() + "/" + pair.get(2).getValue());
				pid = pair.get(2).getValue();
				arrayAdapter = (ArrayAdapter<Comment>) (params[0].get(2));
				publishProgress(user.addComment(pair));
			} else if (Integer.parseInt(params[0].get(0).toString()) == signup_tag) {
				List<NameValuePair> pair = (List<NameValuePair>) params[0]
						.get(1);
				tag = signup_tag;
				// flag=user.signUp(pair, contextParent);
				publishProgress(user.signUp(pair));
			} else if (Integer.parseInt(params[0].get(0).toString()) == addplace_tag) {
				List<NameValuePair> pair = (List<NameValuePair>) params[0]
						.get(1);
				System.out.println("place name:" + pair.get(0).getValue());
				tag = addplace_tag;
				// flag=user.addPlace(pair);
				publishProgress(user.addPlace(pair));
			} else if (Integer.parseInt(params[0].get(0).toString()) == addfavorite_tag) {
				List<NameValuePair> pair = (List<NameValuePair>) params[0]
						.get(1);
				tag = addfavorite_tag;
				// flag=user.addPlace(pair);
				Log.v("favorite add", pair.get(0).getName() + "/"
						+ pair.get(0).getValue());
				publishProgress(user.addFavorite(pair));
			} else if (Integer.parseInt(params[0].get(0).toString()) == edituser_tag) {
				List<NameValuePair> pair = (List<NameValuePair>) params[0]
						.get(1);
				tag = edituser_tag;
				user.setToken(params[0].get(2).toString());
				// user.setUserID(Integer.parseInt(params[0].get(2).toString()));
				Log.v("log edit token", params[0].get(2).toString());
				publishProgress(user.updateInfoUser(pair));

				for (int i = 0; i < pair.size(); i++)
					Log.v("log edit", pair.get(i).getName() + "/"
							+ pair.get(i).getValue());
			} else if (Integer.parseInt(params[0].get(0).toString()) == report_tag) {
				List<NameValuePair> pair = (List<NameValuePair>) params[0]
						.get(1);
				tag = report_tag;
				// flag=user.addPlace(pair);
				for (int i = 0; i < pair.size(); i++)
					Log.v("log report",
							pair.get(i).getName() + "/"
									+ pair.get(i).getValue());
				publishProgress(user.sendReport(pair));
			}
		} else {
			publishProgress(-404);
			tag = -404;
		}
		return null;
	}

	@Override
	protected void onProgressUpdate(Integer... values) {

		if (tag == login_tag) {
			if (values[0] == success_tag) {
				contextParent.finish();

				Intent searchIntent = new Intent(contextParent,
						SearchActivity.class);
				contextParent.startActivity(searchIntent);
				Button bt = (Button) contextParent.findViewById(R.id.btLogin);

				bt.setEnabled(true);
			} else if (values[0] == 2) {
				Toast toast = Toast.makeText(contextParent,
						"Tài khoản của bạn đang bị khóa!", 1000);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				Button bt = (Button) contextParent.findViewById(R.id.btLogin);

				bt.setEnabled(true);
			} else {

				Toast toast = Toast.makeText(contextParent,
						"Email hoặc mật khẩu không đúng!", 1000);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				Button bt = (Button) contextParent.findViewById(R.id.btLogin);

				// SystemClock.sleep(5000);
				bt.setEnabled(true);
			}
		}
		if (tag == addplace_tag) {
			if (values[0] == success_tag) {
				Toast toast = Toast.makeText(contextParent,
						"Thêm địa điểm thành công!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else {
				Toast toast = Toast.makeText(contextParent,
						"Xảy ra lỗi, vui lòng thử lại sau!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		}
		if (tag == edituser_tag) {
			if (values[0] == success_tag) {
				contextParent.findViewById(R.id.btSave).setEnabled(true);
				Toast toast = Toast.makeText(contextParent,
						"Cập nhật thông tin người dùng thành công!",
						Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else {
				contextParent.findViewById(R.id.btSave).setEnabled(true);
				Toast toast = Toast.makeText(contextParent,
						"Xảy ra lỗi vui lòng thử lại sau!!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		}
		if (tag == addfavorite_tag) {
			if (values[0] == success_tag) {
				Toast toast = Toast.makeText(contextParent,
						"Thêm favorite thành công!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				contextParent.findViewById(R.id.btaddFavorite).setVisibility(
						View.GONE);

			} else {
				Toast toast = Toast
						.makeText(contextParent,
								"Xảy ra lỗi, vui lòng thử lại sau!!",
								Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		}
		if (tag == forgotpw_tag) {
			Log.v("reset error", values[0] + "");
			if (values[0] == success_tag) {
				Toast toast = Toast.makeText(contextParent,
						"Kiểm tra email để nhận mật khẩu mới!",
						Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else {
				Toast toast = Toast.makeText(contextParent,
						"Email này không tồn tại!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
			Button bt = (Button) contextParent.findViewById(R.id.btResetPw);
			bt.setEnabled(true);
		}
		if (tag == addComment_tag) {

			if (values[0] == success_tag) {
				/*
				 * Toast toast=Toast.makeText(contextParent, "Success!",
				 * Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0);
				 * toast.show();
				 */
				EditText et = (EditText) contextParent
						.findViewById(R.id.txtCommentContent);
				et.setText("");
				DoThreadGet reloadComment = new DoThreadGet(contextParent);
				ArrayList paramsComment = new ArrayList();
				paramsComment.add(getComment_tag);
				paramsComment.add(pid);
				paramsComment.add(arrayAdapter);
				reloadComment.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR,
						paramsComment);
			} else {
				Toast toast = Toast.makeText(contextParent,
						"Xảy ra lỗi vui lòng thử lại sau!!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		}
		if (tag == report_tag) {
			if (values[0] == success_tag) {
				Toast toast = Toast.makeText(contextParent,
						"Báo cáo sai phạm thành công!", 1000);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else if (values[0] == 1) {
				Toast toast = Toast.makeText(contextParent,
						"Xảy ra lỗi vui lòng thử lại sau!!", 1000);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}

		}
		if (tag == addVote_tag) {
			if (values[0] == 0) {

				TextView tv = (TextView) contextParent
						.findViewById(R.id.tvVotePoint);
				tv.setText(user.getVotePoint());
				Toast toast = Toast.makeText(contextParent,
						"Bình chọn thành công!", 1000);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				ImageView iv = (ImageView) contextParent
						.findViewById(R.id.ivVotePoitn);

				contextParent.findViewById(R.id.btVote1).setVisibility(
						View.GONE);
				contextParent.findViewById(R.id.spVote)
						.setVisibility(View.GONE);
				double votePoint = Double.parseDouble(user.getVotePoint());
				if (votePoint == 0) {
					iv.setImageResource(R.drawable.s0);
				}
				if (votePoint <= 1.5 && votePoint > 0) {
					iv.setImageResource(R.drawable.s11);
				}
				if (votePoint < 2.5 && votePoint > 1.5) {
					iv.setImageResource(R.drawable.s2);
				}
				if (votePoint < 3.5 && votePoint > 2.5) {
					iv.setImageResource(R.drawable.s3);
				}
				if (votePoint < 4.5 && votePoint > 3.5) {
					iv.setImageResource(R.drawable.s4);
				}
				if (votePoint == 5) {
					iv.setImageResource(R.drawable.s5);
				}
			} else if (values[0] == 1) {
				Toast toast = Toast.makeText(contextParent,
						"Xảy ra lỗi vui lòng thử lại sau!!", 1000);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}

		}
		if (tag == signup_tag) {
			if (values[0] == success_tag) {
				Toast toast = Toast.makeText(contextParent,
						"Đăng kí thành công!", 1000);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				Intent profileIntent = new Intent(contextParent,
						SearchActivity.class);
				contextParent.startActivity(profileIntent);
			} else if (values[0] == 1) {
				Toast toast = Toast.makeText(contextParent,
						"Nhắc lại mật khẩu không đúng!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else if (values[0] == 2) {
				Toast toast = Toast.makeText(contextParent,
						"Email không đúng!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else if (values[0] == 3) {
				Toast toast = Toast.makeText(contextParent,
						"Email đã được sử dụng!", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		}
		if (tag == -404) {
			Toast toast = Toast.makeText(contextParent,
					"Vui lòng kiểm tra đường truyền!", 500);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			Button bt = (Button) contextParent.findViewById(R.id.btLogin);

			bt.setEnabled(true);
		}
		if (progress.isShowing())
			progress.dismiss();

	}
	
}
